#include <task.h>
#include "task_desc.h"

typedef struct {
  void    *(*function) (void *);
  void      *arg;
  task_t     task;
} args_t;

/* This function wrap the task function in order to needed initial and
 * the final cooperation. The actual task function, its arguments and
 * the task descriptor is received from 'arg'
 */
void *task_wrapper (void *arg) {
  args_t args = *(args_t *)arg;
  free (arg);

  task_start (args.task);
  args.function (args.arg);
  task_finish ();

  return NULL;
}

void task_wrap_start_routine (task_t task,
			      void *(**start_routine) (void *), void **arg) {
  args_t *wrapper_args = (args_t *) malloc (sizeof (args_t));
  wrapper_args->task     = task;
  wrapper_args->function = *start_routine;
  wrapper_args->arg      = *arg;

  *start_routine = task_wrapper;
  *arg           = wrapper_args;
}
